/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.ts.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.AuditSciPersonnelInfoDto;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.SciPersonnelInfoAddDto;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.SciPersonnelInfoDto;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.SciPersonnelInfoQueryCriteria;
import com.fzhucc.modules.sciTechnologyDemand.domain.dto.AllowSciTechnologyDemandDto;
import com.fzhucc.modules.ts.domain.TsTeam;
import com.fzhucc.modules.ts.domain.dto.*;
import com.fzhucc.modules.ts.service.TsTeamService;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2023-12-07
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "技术服务队管理")
@RequestMapping("/api/tsTeam")
public class TsTeamController {

    private final TsTeamService tsTeamService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('tsTeam:list')")
    public void exportTsTeam(HttpServletResponse response, TsTeamQueryCriteria criteria) throws IOException {
        tsTeamService.download(tsTeamService.queryAll(criteria), response);
    }

    @Log("我的导出数据")
    @ApiOperation("我的导出数据")
    @GetMapping(value = "/my/download")
    @PreAuthorize("@el.check('tsTeam:list')")
    public void myexportTsTeam(HttpServletResponse response, TsTeamQueryCriteria criteria) throws IOException {
        tsTeamService.download(tsTeamService.queryAllmy(criteria), response);
    }

    @GetMapping
    @Log("查询技术服务队")
    @ApiOperation("查询技术服务队")
    @PreAuthorize("@el.check('tsTeam:list')")
    public ResponseEntity<Object> queryTsTeam(TsTeamQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tsTeamService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/my")
    @Log("查询我的技术服务队")
    @ApiOperation("查询我的技术服务队")
    //@PreAuthorize("@el.check('tsTeam:list')")
    public ResponseEntity<Object> my(TsTeamQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tsTeamService.my(criteria,pageable), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/list")
    @Log("h5、小程序查询技术服务队管理")
    @ApiOperation("h5、小程序查询技术服务队管理")
    public ResponseEntity<Object> list(TsTeamQueryCriteria criteria, Pageable pageable){
        criteria.setIsShow(1);
        return new ResponseEntity<>(tsTeamService.queryAllh5(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增技术服务队")
    @ApiOperation("新增技术服务队")
    @PreAuthorize("@el.check('tsTeam:add')")
    public ResponseEntity<Object> createTsTeam(@Validated @RequestBody TsTeamAddDto dto){
        return new ResponseEntity<>(tsTeamService.create(dto), HttpStatus.CREATED);
    }

    @PutMapping(value = "/isAllowEdit")
    @Log("是否允许编辑")
    @ApiOperation("是否允许编辑")
    public ResponseEntity<Object> isAllowEdit(@Validated @RequestBody AllowTsTeamDto dto) {
        tsTeamService.isAllowEdit(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping
    @Log("修改技术服务队")
    @ApiOperation("修改技术服务队")
    @PreAuthorize("@el.check('tsTeam:edit')")
    public ResponseEntity<Object> updateTsTeam(@Validated @RequestBody TsTeamEditDto dto){
        tsTeamService.update(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除技术服务队")
    @ApiOperation("删除技术服务队")
    @PreAuthorize("@el.check('tsTeam:del')")
    public ResponseEntity<Object> deleteTsTeam(@RequestBody Integer[] ids) {
        tsTeamService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping(value = "/audit")
    @Log("技术服务队管理表状态审核")
    @ApiOperation("技术服务队管理表状态审核")
    public ResponseEntity<Object> audit(@Validated @RequestBody TsInfoDto dto){
        tsTeamService.audit(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping(value = "/getTTA")
    @Log("获取字典technology_team-area")
    @ApiOperation("获取字典technology_team-area")
    @AnonymousAccess
    public ResponseEntity<Object> getTTA() {
        return new ResponseEntity<>(tsTeamService.getTTA(),HttpStatus.OK);
    }

    @GetMapping(value = "/getTTM")
    @Log("获取字典technology_team-main")
    @ApiOperation("获取字典technology_team-main")
    @AnonymousAccess
    public ResponseEntity<Object> getTTM() {
        return new ResponseEntity<>(tsTeamService.getTTM(),HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个技术服务队")
    @ApiOperation("查询单个技术服务队")
    public TsTeamDto queryInfoById(@PathVariable Integer id) {
        return tsTeamService.findById(id);
    }

    @PostMapping(value = "/addWithSaveSci")
    @Log("小程序、h5新增技术服务队")
    @ApiOperation("小程序、h5新增技术服务队")
    public ResponseEntity<Object> createAll(@Validated @RequestBody TsTeamAddDto dto){
        return new ResponseEntity<>(tsTeamService.createAll(dto), HttpStatus.CREATED);
    }



    @AnonymousAccess
    @GetMapping(value = "/cityCountyLevelList")
    @Log("h5、小程序查询技术服务队市级区县列表")
    @ApiOperation("h5、小程序查询技术服务队市级区县列表")
    public ResponseEntity<Object> cityCountyLevelList(TsTeamCityCountyQueryCriteria criteria){
        return new ResponseEntity<>(tsTeamService.cityCountyLevelList2(criteria), HttpStatus.OK);
    }

}